/*
 * CfgParams.c
 *
 *  Created on: 10 Mar 2011
 *      Author: Bagrat Aznauryan
 */

#include "Parameters.h"
#include "file_system.h"
#include "AntelCfgOID.h"

unsigned char CfgFileExist;


void CfgRoot_init_Default(void)
{
	ConfigRoot.G_Oper.SG_Data_Interface.param_primary_interface_select = 0; //select UART
	ConfigRoot.G_Oper.SG_Data_Interface.param_interface_timeout = 0;			//default Protocol Timeout 0
	ConfigRoot.G_Oper.SG_Data_Interface.param_baud_rate = 115200;
	ConfigRoot.G_Oper.SG_Data_Interface.param_parity = 1;
	ConfigRoot.G_Oper.SG_Data_Interface.param_data_bits = 8;
	ConfigRoot.G_Oper.SG_Data_Interface.param_flow_control = 2;
	ConfigRoot.G_Oper.SG_Data_Interface.param_stop_bits = 2;

	ConfigRoot.G_Wireless.SG_Uhf.param_channel_num = 1;
	ConfigRoot.G_Wireless.SG_Uhf.param_output_pow = 18;					//default power value 10dbm
	ConfigRoot.G_Wireless.SG_Uhf.param_modulation = 3;					//default modulation 64QAM
	ConfigRoot.G_Wireless.SG_Uhf.param_fec = 0;							//default FEC Off
	ConfigRoot.G_Wireless.SG_Uhf.param_scram = 0;						//default Protocol Scram 0
	ConfigRoot.G_Wireless.SG_Uhf.param_carrier_freq_number = 1;		//default Protocol Scram 800
	ConfigRoot.G_Wireless.SG_Uhf.param_freq_number_symbolrate = 0;
	//start initialization network settings
	ConfigRoot.G_Network.SG_NetProtSet.param_bridging_reg.bridging_protocol = 0;
	ConfigRoot.G_Network.SG_NetProtSet.param_bridging_reg.reserved = 0;
	ConfigRoot.G_Network.SG_NetProtSet.param_bridging_reg.pmp = 0;
	ConfigRoot.G_Network.SG_NetProtSet.param_snmp_protocol_reg.snmp_protocol_selector = 0;
	ConfigRoot.G_Network.SG_NetProtSet.param_snmp_protocol_reg.reserved = 0;
	ConfigRoot.G_Network.SG_NetProtSet.param_snmp_protocol_reg.trap_timeout_en_dis = 0;
	ConfigRoot.G_Network.SG_NetProtSet.param_snmp_traptimeout = 0;
	ConfigRoot.G_Network.SG_WiFiSet.network_register.reserved = 0;
	ConfigRoot.G_Network.SG_WiFiSet.network_register.conn_even = 0;
	ConfigRoot.G_Network.SG_WiFiSet.network_register.conn_preferred = 0;
	ConfigRoot.G_Network.SG_WiFiSet.network_register.conn_automaticaly = 0;
	ConfigRoot.G_Network.SG_WiFiSet.network_register.wifi_network_type = 0;
	ConfigRoot.G_Network.SG_WiFiSet.access_point_name[0] = '\0';
	ConfigRoot.G_Network.SG_WiFiSet.ssid[0] = '\0';
	ConfigRoot.G_Network.SG_TCPIPSet.param_tcpip_reg.reserved = 0;
	ConfigRoot.G_Network.SG_TCPIPSet.param_tcpip_reg.dhcp = 0;
	ConfigRoot.G_Network.SG_TCPIPSet.param_ip_address.addr = 0;//???????
	ConfigRoot.G_Network.SG_TCPIPSet.param_subnet_mask.addr = 0;//??????
	ConfigRoot.G_Network.SG_TCPIPSet.param_default_gateway.addr = 0;//???
	ConfigRoot.G_Network.SG_TCPIPSet.param_dns_server.addr = 0;//????????
	ConfigRoot.G_Network.SG_NetSec.security_type = 0;
	ConfigRoot.G_Network.SG_NetSec.encryption_type = 0;
	ConfigRoot.G_Network.SG_NetSec.network_security_key[0] = '\0';
	//end of initialization network settings

	Current_Channel.freq = 450000000;									//default carrier frequency
	Current_Channel.pow = 31;											//Max power 32dbm
	Current_Channel.sym_rate = 0;										//channel spacing

	CfgFileExist = 0;
}

void CfgRoot_init(void)
{
	unsigned char buffer[SIZEOF_FILEHEADER];
	CfgFileExist = 1;
	Init_Param_Values();
	if(gBoot_option)
	{
		at25dfx_read_bytes(files_list[FCFG_FILE_INDEX].sector_number * AT25DFX_SECTOR_SIZE,SIZEOF_FILEHEADER,buffer);
		if(!check_file_header(buffer,files_list[FCFG_FILE_INDEX]))
		{
			CfgRoot_init_Default();
			init_code_params();
			return;
		}
	}
	else
	{
		at25dfx_read_bytes(files_list[UCFG_FILE_INDEX].sector_number * AT25DFX_SECTOR_SIZE,SIZEOF_FILEHEADER,buffer);
		if(!check_file_header(buffer,files_list[UCFG_FILE_INDEX]))
		{
			CfgRoot_init_Default();
			init_code_params();
			return;
		}
	}

	USER_OID_USER_NAME(&ConfigRoot.G_Oper.SG_DeviceIndividualization.param_user);
	USER_OID_UNIT_NAME(&ConfigRoot.G_Oper.SG_DeviceIndividualization.param_unit);
	USER_OID_LOCATION(&ConfigRoot.G_Oper.SG_DeviceIndividualization.param_loc);

	USER_OID_PRIMARY_INTERFACE_SELECT(&(ConfigRoot.G_Oper.SG_Data_Interface.param_primary_interface_select));
	USER_OID_INTERFACE_TIMEOUT(&(ConfigRoot.G_Oper.SG_Data_Interface.param_interface_timeout));
	USER_OID_ASYNCHRONOUS_BAUDRATE(&(ConfigRoot.G_Oper.SG_Data_Interface.param_baud_rate));
	USER_OID_ASYNCHRONOUS_DATABITS(&(ConfigRoot.G_Oper.SG_Data_Interface.param_data_bits));
	USER_OID_ASYNCHRONOUS_PARITY(&(ConfigRoot.G_Oper.SG_Data_Interface.param_parity));
	USER_OID_ASYNCHRONOUS_STOPBITS(&(ConfigRoot.G_Oper.SG_Data_Interface.param_stop_bits));
	USER_OID_ASYNCHRONOUS_FLOWCONTROL(&(ConfigRoot.G_Oper.SG_Data_Interface.param_flow_control));

	USER_OID_SLEEP_MODE_REG(&(ConfigRoot.G_Oper.SG_SleepMode.param_sleep_reg));
	USER_OID_QUIET_TIMEOUT(&(ConfigRoot.G_Oper.SG_SleepMode.param_quit));
	USER_OID_WAKEUP_TIMEOUT(&(ConfigRoot.G_Oper.SG_SleepMode.param_wakeup));
	if(ConfigRoot.G_Oper.SG_SleepMode.param_sleep_reg > 7)
		ConfigRoot.G_Oper.SG_SleepMode.param_sleep_reg = 0;

	USER_OID_CHANNEL_NUMBER(&(ConfigRoot.G_Wireless.SG_Uhf.param_channel_num));
	if(ConfigRoot.G_Wireless.SG_Uhf.param_channel_num < 1 || ConfigRoot.G_Wireless.SG_Uhf.param_channel_num > 32)
		ConfigRoot.G_Wireless.SG_Uhf.param_channel_num  = 1;
	USER_OID_FREQUENCY_CARRIER_NUMBER(&Current_Channel.freq, ConfigRoot.G_Wireless.SG_Uhf.param_channel_num);
	USER_OID_OUTPUT_POWER(&Current_Channel.pow, ConfigRoot.G_Wireless.SG_Uhf.param_channel_num);
	USER_OID_SYMBOL_RATE(&Current_Channel.sym_rate, ConfigRoot.G_Wireless.SG_Uhf.param_channel_num);

	USER_OID_OUTPUT_POWER_WL(&(ConfigRoot.G_Wireless.SG_Uhf.param_output_pow));
	USER_OID_MODULATION_TYPE(&(ConfigRoot.G_Wireless.SG_Uhf.param_modulation));
	USER_OID_FEC_TYPE(&(ConfigRoot.G_Wireless.SG_Uhf.param_fec));
	USER_OID_SCRAM(&(ConfigRoot.G_Wireless.SG_Uhf.param_scram));
	USER_OID_CARRIER_FREQ_NUMBER(&(ConfigRoot.G_Wireless.SG_Uhf.param_carrier_freq_number));
	USER_OID_FREQ_NUMBER_SYMBOLRATE(&(ConfigRoot.G_Wireless.SG_Uhf.param_freq_number_symbolrate));
	//start initialization network settings
	USER_OID_BRIDGING_PROTOCOL(&(ConfigRoot.G_Network.SG_NetProtSet.param_bridging_reg));
	USER_OID_SNMP_PROTOCOL(&(ConfigRoot.G_Network.SG_NetProtSet.param_snmp_protocol_reg));
	USER_OID_SNMP_TRAP_TIMEOUT(&(ConfigRoot.G_Network.SG_NetProtSet.param_snmp_traptimeout));

	USER_OID_NETWORK_REGISTER(&(ConfigRoot.G_Network.SG_WiFiSet.network_register));
	USER_OID_ACCESS_POINT_NAME(&(ConfigRoot.G_Network.SG_WiFiSet.access_point_name));
	USER_OID_SSID(&(ConfigRoot.G_Network.SG_WiFiSet.ssid));

	USER_OID_TCPIP_REGISTER(&(ConfigRoot.G_Network.SG_TCPIPSet.param_tcpip_reg));
	USER_OID_IP_ADDRESS(&(ConfigRoot.G_Network.SG_TCPIPSet.param_ip_address));
	USER_OID_SUBNET_MASK(&(ConfigRoot.G_Network.SG_TCPIPSet.param_subnet_mask));
	USER_OID_DEFAULT_GATEWAY(&(ConfigRoot.G_Network.SG_TCPIPSet.param_default_gateway));
	USER_OID_DNS_SERVER(&(ConfigRoot.G_Network.SG_TCPIPSet.param_dns_server));

	USER_OID_SECURITY_TYPE(&(ConfigRoot.G_Network.SG_NetSec.security_type));
	USER_OID_ENCRYPTION_TYPE(&(ConfigRoot.G_Network.SG_NetSec.encryption_type));
	USER_OID_SECURITY_KEY(&(ConfigRoot.G_Network.SG_NetSec.network_security_key));
	//end of initialization network settings

	//////////////////////////////////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////////////////////

//CBTB_OID_FUNCTIONAL(ptr_cfg);// 1.1.x
char x1, x2, x3, x4, x8, x9, x10, x11;
unsigned int x5, x15, x16, x19[32];

short x6, x17[16], x18[126];
unsigned short x12, x13;

CBTB_OID_MAX_OUTPUT_POWER(&x1);// 1.1.1
CBTB_OID_MIN_OUTPUT_POWER(&x2);// 1.1.2
CBTB_OID_NUMBER_OF_FREQUENCY_SUB_BANDS(&x3);// 1.1.3
CBTB_OID_NUMBER_OF_OUTPUT_POWER_LEVELS(&x4);// 1.1.4
CBTB_OID_STOP_FREQBAND(&x5);// 1.1.5
CBTB_OID_FREQERR(&x6);// 1.1.6

//CBTB_OID_TRANSMITER_OPL(ptr_cfg,N,M);

CBTB_OUD_OUTPUT_POWER(&x8,2,1);// 2*(N-1)+2.M.1
CBTB_OUD_FWD_ATTENUATOR(&x9,2,1);// 2*(N-1)+2.M.2
CBTB_OUD_FEEDBACK_ATTENUATOR(&x10,8,8);// 2*(N-1)+2.M.3
CBTB_OUD_PHASE_SHIFT(&x11,8,8);// 2*(N-1)+2.M.4
CBTB_OUD_VAPC(&x12,8,8);// 2*(N-1)+2.M.5
CBTB_OUD_BASEBAND_ATTENUATOR(&x13,8,8);// 2*(N-1)+2.M.6

//CBTB_OID_RECEIVER_RSSI(ptr_cfg,N);

CBTB_OUD_SUB_BAND_START_FREQ(&x15,1);// 2*(N-1)+2.1.1
CBTB_OUD_RECEIVER_SYSTEM_GAIN(&x16,1);// 2*(N-1)+2.1.2
CBTB_OUD_AGC_TABLE(&x17,1);// 2*(N-1)+2.1.3
CBTB_OUD_CHANNEL_EQUALIZATION_TABLE(&x18,1);// 2*(N-1)+2.1.4
CBTB_OUD_BASEBAND_AMPLIFIER_TABLE(&x19,1);// 2*(N-1)+2.1.5
///////////////////////////////////////////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////////////////////

	init_code_params();
}

